package com.nl.lib_project.view.switchbutton

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.RelativeLayout
import androidx.annotation.DrawableRes
import com.nl.lib_core.tool.coroutines.FunctionTypeIntUnit
import com.nl.lib_core.tool.extends.dp2px
import com.nl.lib_project.R

/** 作者: wangfei 创建时间: 2021/9/29 包名: com.nl.lib_project.view 类名: SwitchButton */
class SwitchButton : RelativeLayout {

  init {
    inflate(context, R.layout.custom_switch_button, this)
    mRlRoot = findViewById(R.id.rl_root)
    mSbButton = findViewById(R.id.sb_button)
  }

  private var mRlRoot: RelativeLayout? = null
  private var mRLWidth = 0f
  private var mRLHeight = 0f
  private var mSbButton: View? = null
  private var mSBWidth = 0f
  private var mSBHeight = 0f
  private var array: TypedArray? = null
  private var mDefaultStatus: Int = 0
  private var mOnSwitchListener: FunctionTypeIntUnit? = null
  @DrawableRes private var mButtonOffBg: Int = 0
  @DrawableRes private var mButtonOnBg: Int = 0
  private val close = 0
  private val open = 1

  constructor(context: Context?) : super(context)
  constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
    init(attrs)
  }
  constructor(
      context: Context?,
      attrs: AttributeSet?,
      defStyleAttr: Int
  ) : super(context, attrs, defStyleAttr) {
    init(attrs)
  }

  /** 初始化操作 */
  private fun init(attrs: AttributeSet?) {
    array = context!!.obtainStyledAttributes(attrs, R.styleable.SwitchButton, 0, 0)
    mDefaultStatus = array!!.getInt(R.styleable.SwitchButton_defaultStatus, 1)
    mButtonOffBg = array!!.getResourceId(R.styleable.SwitchButton_button_off_bg, 0)
    mButtonOnBg = array!!.getResourceId(R.styleable.SwitchButton_button_on_bg, 0)
    mRLWidth = array!!.getFloat(R.styleable.SwitchButton_root_width, 0f)
    mRLHeight = array!!.getFloat(R.styleable.SwitchButton_root_height, 0f)
    mSBWidth = array!!.getFloat(R.styleable.SwitchButton_button_width, 0f)
    mSBHeight = array!!.getFloat(R.styleable.SwitchButton_button_height, 0f)
    mRlRoot?.apply {
      setBackgroundResource(array!!.getResourceId(R.styleable.SwitchButton_root_bg, 0))
      val layoutParam = LayoutParams(context.dp2px(mRLWidth), context.dp2px(mRLHeight))
      layoutParams = layoutParam
    }
    mSbButton?.apply {
      val layoutParam = LayoutParams(context.dp2px(mSBWidth), context.dp2px(mSBHeight))
      layoutParams = layoutParam
    }
    // 0是关闭 1是打开
    array!!.recycle()
  }

  fun initButtonStatue(type: Int) {
    if (type == 0) {
      closeButton()
    } else if (type == 1) {
      openButton()
    }
  }

  private fun setViewLocation(view: View, location: Int) {
    val layoutParams = view.layoutParams as LayoutParams
    if (location == ALIGN_PARENT_RIGHT) {
      layoutParams.removeRule(ALIGN_PARENT_LEFT)
    } else if (location == ALIGN_PARENT_LEFT) {
      layoutParams.removeRule(ALIGN_PARENT_RIGHT)
    }
    layoutParams.addRule(location)
    view.layoutParams = layoutParams
  }

  /** 关闭按钮 */
  private fun closeButton() {
    mSbButton!!.setBackgroundResource(mButtonOffBg)
    setViewLocation(mSbButton!!, ALIGN_PARENT_LEFT)
    mDefaultStatus = close
  }

  /** 打开按钮 */
  private fun openButton() {
    mSbButton!!.setBackgroundResource(mButtonOnBg)
    setViewLocation(mSbButton!!, ALIGN_PARENT_RIGHT)
    mDefaultStatus = open
  }

  /** 改变状态 */
  private fun changeStatus() {
    if (mDefaultStatus == open) {
      closeButton()
      mOnSwitchListener?.invoke(SwitchButtonState.CLOSE_BUTTON)
    } else if (mDefaultStatus == close) {
      openButton()
      mOnSwitchListener?.invoke(SwitchButtonState.OPEN_BUTTON)
    }
  }

  @SuppressLint("ClickableViewAccessibility")
  override fun onTouchEvent(event: MotionEvent?): Boolean {
    when (event!!.action) {
      MotionEvent.ACTION_DOWN -> {
        changeStatus()
      }
    }
    return super.onTouchEvent(event)
  }

  /** 设置状态变化监听 */
  fun setOnSwitchListener(onSwitchListener: FunctionTypeIntUnit) {
    this.mOnSwitchListener = onSwitchListener
  }
}
